package http;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import app.MyApp;
import bean.Cfpb;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import utils.ToastUtil;

public class RetrofitUtil{
    public static String sqlCfpb;

    private static Context mContext;
    static {
        mContext = MyApp.getContext();
    }
    public static RetrofitApi getRetrofitApi(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.229:2233/")
                .build();
        RetrofitApi api = retrofit.create(RetrofitApi.class);
        return api;
    }

    public static RetrofitApi getRetrofitApi2(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.229:2233/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitApi api = retrofit.create(RetrofitApi.class);
        return api;
    }

    public static Map getParams(String state,String sql){
        Map<String, String> params = new HashMap<>();
        String base64 = getBase64(sql).replaceAll("\n", "");
        params.put("State", state);
        params.put("Ssql", base64);
        return params;
    }
    public static String getBase64(String str){
        byte[] b = null;
        String s = null;
        try{
            b = str.getBytes("utf-8");
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }
        if(b != null){
            s= android.util.Base64.encodeToString(str.getBytes(), android.util.Base64.DEFAULT);
        }
        return s;
    }

    public static void postResponse(String state,String sql){
        getRetrofitApi().getResponse(getParams(state, sql)).enqueue(new Callback<ResponseBody>(){
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.e("response====",response.body().string());
                } catch (IOException e) {
                    Log.e("e==",e+"");
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                ToastUtil.showShort(mContext,t+"");
            }
        });
    }

    public static void postEnqueue6(String sql,Callback callback){
        Call call = getRetrofitApi().getResponse(getParams("6", sql));
        call.enqueue(callback);
    }

    public static <T>List<T> postExecute(String sql, final Activity activity, Class<T>cls){
        Call call = getRetrofitApi().getResponse(getParams("6", sql));
        String result=null;
        try {
            ResponseBody body = (ResponseBody) call.execute().body();
            result = body.string();
            if(result.equals("]")){
                return Collections.emptyList();
            }else{
                ListParameterizedType type = new ListParameterizedType(cls);
                Gson gson = new Gson();
                return gson.fromJson(result, type);
            }
        } catch (final IOException e) {
            activity.runOnUiThread(() -> ToastUtil.showShort(activity, e+""));
            return Collections.emptyList();
        }catch (JsonSyntaxException e){
            final String finalResult = result;
            activity.runOnUiThread(() -> ToastUtil.showShort(activity, finalResult +""));
            return Collections.emptyList();
        }
    }
}
